package com.olda.play.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.olda.play.constant.Constant;
import com.olda.play.enums.ResponseCode;
import com.olda.play.model.DropOption;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysMenuBizService;
import com.olda.play.system.biz.dao.SysMenuBizMapper;
import com.olda.play.system.dto.SysMenuDto;
import com.olda.play.system.model.po.SysMenu;
import com.olda.play.system.service.SysMenuService;
import com.olda.play.system.util.SysUtils;
import com.olda.play.system.vo.SysMenuVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 菜单业务接口
 * @author oldA
 */
@Service
@RequestMapping(value =  "/menu")
@RestController
public class SysMenuServiceImpl  implements SysMenuService {

    @Autowired
    private SysMenuBizMapper sysMenuBizMapper;
    
    @Autowired
    private SysMenuBizService sysMenuBizService;

    /**
     * 不分页获取菜单列表
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysMenuList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<SysMenuDto>> querySysMenuList(@RequestBody SysMenuVo param) {
        List<SysMenuDto> list=sysMenuBizService.querySysMenuList(param);
        return CommonResponseDto.success(SysUtils.generateRecurMenus(list));
    }

    /**
     * 新增或修改菜单
     * @param param
     * @return
     */
    @Override
    @RequestMapping(value = "/addOrUpdateMenu",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> saveMenu(@RequestBody SysMenuVo param) {
        if(param==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Long id=param.getId();
        if(id==null){
            SysMenu sysMenu=BeanUtil.copyProperties(param,SysMenu.class);
            sysMenuBizService.save(sysMenu);
        }else{
            SysMenu sysMenu=BeanUtil.copyProperties(param,SysMenu.class);
            sysMenuBizService.updateSysMenu(sysMenu);
        }
        return CommonResponseDto.success(true);
    }

    /**
     * 新增菜单时获取下拉菜单树
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysMenuDropOption",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<DropOption>> querySysMenuDropOption() {
        SysMenuVo param=new SysMenuVo();
        param.setTypes(Arrays.asList(0,1));
        param.setStatus(1);
        List<SysMenuDto> list=sysMenuBizService.querySysMenuList(param);
        return CommonResponseDto.success(SysUtils.recurDropMenus(Constant.ROOT_MENU_ID,list));
    }

    /**
     * 用户登录成功后获取其菜单结构
     * @return
     */
    @Override
    @RequestMapping(value = "/getUserRoleMenuList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<SysMenuDto>> getUserRoleMenuList() {
        SysMenuVo param=new SysMenuVo();
        param.setTypes(Arrays.asList(0,1));
        param.setStatus(1);
        List<SysMenuDto> list=sysMenuBizService.getUserRoleMenuList(param);
        return CommonResponseDto.success(list);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteSysMenuById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> deleteByIds(Long id) {
        sysMenuBizService.deleteSysMenuById(id);
        return CommonResponseDto.success(true);
    }
}
